class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head==NULL){
          return false;
        }
        ListNode *fast=head->next,*slow=head;
        if(fast==NULL||fast->next==NULL){
            return false;
        }
        while(fast!=slow){
          if(fast==NULL||fast->next==NULL){
              return false;
          }
            fast=fast->next->next;
            slow=slow->next;
        }
        return true;
    }
};
